package com.inossem.warehousestorage_inossem.adapter.analysis;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.inossem.utils.base.Utils;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.analysis.AnalysisWorkStatisticsFindPersonActivity;
import com.inossem.warehousestorage_inossem.bean.analysis.AnalysisWorkStatisticsFindPersonBean;

import java.util.List;

/**
 * 作者：wsy on 2017/11/28 10:31
 * <p>
 * 邮箱：445979770@qq.com
 */

public class WorkStatisticsFindPersonListAdapter extends BaseAdapter {

    private Context mContext;
    private List<AnalysisWorkStatisticsFindPersonBean.DataBean.ReceiverListBean> mReceiverList;
    private LayoutInflater inflater;
    private AnalysisWorkStatisticsFindPersonActivity.AnalysisWorkStatisticsFindPersonListener mAnalysisWorkStatisticsFindPersonListener;
    private String mCondition;

    public WorkStatisticsFindPersonListAdapter(Context context, List<AnalysisWorkStatisticsFindPersonBean.DataBean.ReceiverListBean> receiverList, String condition, AnalysisWorkStatisticsFindPersonActivity.AnalysisWorkStatisticsFindPersonListener analysisWorkStatisticsFindPersonListener) {
        mContext = context;
        mReceiverList = receiverList;
        mAnalysisWorkStatisticsFindPersonListener = analysisWorkStatisticsFindPersonListener;
        inflater = LayoutInflater.from(context);
        mCondition = condition;
    }

    @Override
    public int getCount() {
        return mReceiverList.size() + 1;
    }

    @Override
    public Object getItem(int position) {
        return mReceiverList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (position == 0) {
            convertView = inflater.inflate(R.layout.view_analysis_work_statistics_receiver_list_first_item, null);
            EditText condition = (EditText) convertView.findViewById(R.id.condition);
            ImageView search = (ImageView) convertView.findViewById(R.id.search);
            condition.setText(mCondition == null ? "" : mCondition);
            condition.setHint(R.string.view_analysis_work_statistics_receiver_list_first_item_hint);
            search.setOnClickListener(new NoDoubleClickListener() {
                @Override
                public void onNoDoubleClick(View v) {
                    Utils.closeSoftKeyboard(mContext, v);
                    mAnalysisWorkStatisticsFindPersonListener.onNoDoubleClickListener(condition.getText().toString());
                }
            });
        } else {
            AnalysisWorkStatisticsFindPersonBean.DataBean.ReceiverListBean receiverItem = mReceiverList.get(position - 1);
            WorkStatisticsFindPersonListAdapter.ViewHolder viewHolder;
            if (convertView == null || (convertView != null && convertView.getTag() == null)) {
                convertView = inflater.inflate(R.layout.view_analysis_work_statistics_receiver_list, null);
                viewHolder = new ViewHolder();
                //初始化控件
                initView(viewHolder, convertView);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            //初始化数据
            initData(mContext, viewHolder, convertView, receiverItem);
        }
        return convertView;
    }

    private void initView(WorkStatisticsFindPersonListAdapter.ViewHolder viewHolder, View view) {
        viewHolder.id = (TextView) view.findViewById(R.id.id);
        viewHolder.name = (TextView) view.findViewById(R.id.name);
        viewHolder.userName = (TextView) view.findViewById(R.id.userName);
        viewHolder.company = (TextView) view.findViewById(R.id.company);
        viewHolder.department = (TextView) view.findViewById(R.id.department);
        viewHolder.phone = (TextView) view.findViewById(R.id.phone);
    }

    private void initData(Context context, WorkStatisticsFindPersonListAdapter.ViewHolder viewHolder, View view, AnalysisWorkStatisticsFindPersonBean.DataBean.ReceiverListBean receiverItem) {
        viewHolder.id.setText(receiverItem.getId());
        viewHolder.name.setText(receiverItem.getName());
        viewHolder.userName.setText(receiverItem.getUserName());
        viewHolder.company.setText(receiverItem.getCompany());
        viewHolder.department.setText(receiverItem.getDepartment());
        viewHolder.phone.setText(receiverItem.getPhone());
        view.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                String nameStr = viewHolder.name.getText().toString();
                String idStr = viewHolder.id.getText().toString();
                mAnalysisWorkStatisticsFindPersonListener.selectPerson(nameStr, idStr);
            }
        });
    }

    private class ViewHolder {
        private TextView id, name, userName, company, department, phone;
    }
}
